package com.bsoft.gol.hbs.dao;

import com.bsoft.gol.hbs.utils.CommonUtils;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import hcn.base.BaseDicItem;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.atag.dao.util.BaseDaoImpl;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;

/**
 * Created by pangxl on 2019/12/19.
 */
@SsdevDao(serviceId = "baseDicItemDAOService")
public abstract class BaseDicItemDAO extends BaseDaoImpl<BaseDicItem> {
    public BaseDicItemDAO() {
        this.setKeyField("itemId");
    }

    @RpcService
    @DAOMethod(sql = "update BaseDicItem set status=:status,modify_user=:userId,modify_dt=now() where item_id=:itemId ")
    public abstract void updateEnableByItemId(@DAOParam("status") String status,@DAOParam("userId") String userId, @DAOParam("itemId") int itemId);

    @RpcService
    @DAOMethod(sql = "select count(1) from BaseDicItem where dicId=:dicId ")
    public abstract Long getBaseDicItemCount(@DAOParam("dicId")int dicId);

    @RpcService
    @DAOMethod(sql = " from BaseDicItem where dicId=:dicId and itemCode =:itemCode ")
    public abstract BaseDicItem getBaseDicItemByItemCode(@DAOParam("dicId")int dicId, @DAOParam("itemCode")String itemCode);

    @RpcService
    @DAOMethod(sql = " from BaseDicItem where dicId=:dicId and sort <:sort order by sort desc")
    public abstract List<BaseDicItem> findBaseDicItemBeforeList(@DAOParam("dicId")int dicId, @DAOParam("sort")Short sort);

    @RpcService
    @DAOMethod(sql = " from BaseDicItem where dicId=:dicId and sort >:sort order by sort ")
    public abstract List<BaseDicItem> findBaseDicItemAfterList(@DAOParam("dicId")int dicId, @DAOParam("sort")Short sort);

    @RpcService
    public List<BaseDicItem> findDictionaryItemList(final Integer objectType, final String objectId, final String dicCode) {
        StringBuffer hsql = new StringBuffer();
        hsql.append("SELECT i.item_code as itemCode,i.item_id as itemId,i.item_name as itemName,i.dic_id as dicId from base_dic_item i ");
        hsql.append("LEFT JOIN base_dictionary d on i.dic_id=d.dic_id where i.status=1 ");
         Map<String,Object> params = new HashMap<>();
        if(CommonUtils.isNotEmpty(dicCode)){
            hsql.append(" and d.dic_code=:dicCode");
            params.put("dicCode",dicCode);
        }
        if(CommonUtils.isNotEmpty(objectId)){
            hsql.append(" and d.object_id=:objectId");
            params.put("objectId",objectId);
        }
        if(CommonUtils.isNotEmpty(objectType)){
            hsql.append(" and d.object_type=:objectType");
            params.put("objectType",objectType);
        }
        return super.findListBySql(hsql.toString(),params,BaseDicItem.class);

       /* HibernateStatelessResultAction<List<BaseDicItem>> action = new AbstractHibernateStatelessResultAction<List<BaseDicItem>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("SELECT i.item_code as itemCode,i.item_id as itemId,i.item_name as itemName,i.dic_id as dicId from base_dic_item i ");
                hsql.append("LEFT JOIN base_dictionary d on i.dic_id=d.dic_id where i.status=1 ");
                if(CommonUtils.isNotEmpty(dicCode)){
                    hsql.append(" and d.dic_code='"+dicCode+"'");
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    hsql.append(" and d.object_id='"+objectId+"'");
                }
                if(CommonUtils.isNotEmpty(objectType)){
                    hsql.append(" and d.object_type="+objectType+"");
                }
                Query query = ss.createSQLQuery(hsql.toString());
;                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();
                List<BaseDicItem> ls = new ArrayList<>();
                for (Map map : dbList) {
                    ls.add(ConversionUtils.convert(map, BaseDicItem.class));
                }
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();*/
    }

    @RpcService
    public List<BaseDicItem> findBaseDictionaryItemList(final Integer objectType, final String objectId, final String dicCode) {
        StringBuffer hsql = new StringBuffer();
        hsql.append("SELECT c.item_id as itemId,c.dic_id as dicId, c.item_name as itemName, c.item_code as itemCode FROM base_dictionary a, base_dic_contrast b, base_dic_item c, base_dic_item d  ");
        hsql.append(" WHERE a.dic_id = b.dic_id AND b.base_item_id = c.item_id and c.status='1' AND b.item_id = d.item_id and d.status='1'  ");
        Map<String,Object> params = new HashMap<>();
        if(CommonUtils.isNotEmpty(dicCode)){
            hsql.append(" and d.dic_code=:dicCode");
            params.put("dicCode",dicCode);
        }
        if(CommonUtils.isNotEmpty(objectId)){
            hsql.append(" and d.object_id=:objectId");
            params.put("objectId",objectId);
        }
        if(CommonUtils.isNotEmpty(objectType)){
            hsql.append(" and d.object_type=:objectType");
            params.put("objectType",objectType);
        }
        return super.findListBySql(hsql.toString(),params,BaseDicItem.class);

       /* HibernateStatelessResultAction<List<BaseDicItem>> action = new AbstractHibernateStatelessResultAction<List<BaseDicItem>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("SELECT c.item_id as itemId,c.dic_id as dicId, c.item_name as itemName, c.item_code as itemCode FROM base_dictionary a, base_dic_contrast b, base_dic_item c, base_dic_item d  ");
                hsql.append(" WHERE a.dic_id = b.dic_id AND b.base_item_id = c.item_id and c.status='1' AND b.item_id = d.item_id and d.status='1'  ");
                if(CommonUtils.isNotEmpty(dicCode)){
                    hsql.append(" and  a.dic_code='"+dicCode+"'");
                }
                if(CommonUtils.isNotEmpty(objectId)){
                    hsql.append(" and  a.object_id='"+objectId+"'");
                }
                if(CommonUtils.isNotEmpty(objectType)){
                    hsql.append(" and a.object_type ="+objectType+"");
                }
                Query query = ss.createSQLQuery(hsql.toString());
                query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = query.list();
                List<BaseDicItem> ls = new ArrayList<>();
                for (Map map : dbList) {
                    ls.add(ConversionUtils.convert(map, BaseDicItem.class));
                }
                setResult(ls);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();*/
    }

    @RpcService
    public Integer queryItemCount(final String itemCode, final String objectId, final String dicCode ) {

        StringBuffer hsql = new StringBuffer();
        hsql.append("SELECT count(1) as dicCount from base_dic_item i left JOIN base_dictionary d on i.dic_id=d.dic_id where 1=1 ");
        hsql.append(" and i.item_code=:itemCode");
        hsql.append(" and d.object_id=:objectId ");
        hsql.append(" and d.dic_code=:dicCode ");
        Map<String,Object> params = new HashMap<>();
        params.put("itemCode",itemCode);
        params.put("objectId",objectId);
        params.put("dicCode",dicCode);
        return   this.findForLongBySql(hsql.toString(),params).intValue();
        /*
        HibernateStatelessResultAction<Integer> action = new AbstractHibernateStatelessResultAction<Integer>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("SELECT count(1) as dicCount from base_dic_item i left JOIN base_dictionary d on i.dic_id=d.dic_id where 1=1 ");
                    hsql.append(" and i.item_code='"+itemCode+"'");
                    hsql.append(" and d.object_id='"+objectId+"' ");
                    hsql.append(" and d.dic_code='"+dicCode+"' ");
                Query query = ss.createSQLQuery(hsql.toString());
                
                List<BigInteger> dbList = query.list();
                if (CommonUtils.isNotEmpty(dbList)) {
                    BigInteger bigInteger = dbList.get(0);
                    Integer count = bigInteger.intValue();
                    setResult(count);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();*/
    }


}
